package com.zh.api.controller.third;

import com.zh.common.base.BaseController;
import com.zh.common.domain.UserDO;
import com.zh.common.utils.CheckEmptyUtil;
import com.zh.common.utils.ResultEnum;
import com.zh.common.utils.ResultVO;
import com.zh.third.domain.SiteDO;
import com.zh.third.service.SiteService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @ProjectName: healthCare-parent
 * @Package: com.zh.api.controller.third
 * @ClassName: SiteController
 * @Description: java类作用描述
 * @Author: lds
 * @CreateDate: 2019/6/5 0005 9:18
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/6/5 0005 9:18
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RestController
public class SiteController extends BaseController{
    @Autowired
    private SiteService siteService;

    @RequestMapping(value = "getCurrentSite", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("获取当前收获地址")
    public ResultVO getCurrentSite(@RequestBody Map<String,Object> param){

        if (CheckEmptyUtil.isEmpty(param)) {
            return ResultVO.success(ResultEnum.PARAME_ERROR);
        }

        if (Objects.isNull(param.get("id"))){
            return ResultVO.success(ResultEnum.PARAME_ERROR);
        }

        String id=String.valueOf(param.get("id")+"");
        SiteDO siteDO = siteService.get(id);

        if(siteDO==null){
            return ResultVO.error(ResultEnum.NO_ORDER_ERROR);
        }

        return ResultVO.success(siteDO);
    }

    @RequestMapping(value = "currentSiteUpdate", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("当前收获地址修改")
    public ResultVO  currentSiteUpdate(@RequestBody SiteDO siteDO){
        if (siteDO == null) {
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        if (siteService.update(siteDO) > 0) {
            return ResultVO.success();
        }
        return ResultVO.error();

    }

    @RequestMapping(value = "setDefault", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation("设置默认收货地址")
    public ResultVO setDefault(@RequestBody Map<String,Object> param) {

        UserDO user = getUser();
        if (CheckEmptyUtil.isEmpty(param)) {
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }
        String id = null;
        try {
            id = String.valueOf(param.get("id"));
        } catch (Exception e) {
            logger.error("AppController setDefault has error: " + e.getMessage());
            return ResultVO.error(ResultEnum.PARAME_ERROR);
        }

        SiteDO siteDO = siteService.get(id);
        if (siteDO == null) {
            return ResultVO.error(ResultEnum.NO_SITE_ERROR);
        }

        if (siteService.update(siteDO) > 0) {
            return ResultVO.success();
        }

        return ResultVO.error();
    }
}
